$(document).ready(
	function() {
		$('.page_struct').treeview({
			persist: 'location',
			collapsed: true,
			unique: true
		});
		
		
		jquery_treeview_iterate('page_id', 'page_tree_node');
		
		
		
		$('#short_url_save_button').bind('click', function() {
		
			var oParams = {};
			
			var oPage = $(':radio[name="page_id"]:checked');
		
			if ($(oPage).length === 0) {
				$.prompt('<?php echo addslashes($aMessages['admin_pages_select_redirect']); ?>');
				return;
			}
			
			oParams.sVersionTitle = $('#short_url_title').val();
			oParams.sVersionPath = $('#version_path').val();
			
			oParams.iPageId = $(oPage).val();
			oParams.iVersionId = oShortUrl.version_id;
			oParams.sVersionActive = (($('#version_active:checked').length === 1) ? 'Y' : 'N');
						
			var bSite = false;
			
			var oObj = $(oPage);
			var oParentObj;
			
			while (bSite === false) {
				oParentObj = $(oObj).parent().parent().prev();
				
				if ($(oParentObj).hasClass('site_tree_node') === true) {
					bSite = true;
					oParams.iSiteId = $(oParentObj).attr('id').replace('site_tree_node_', '');
					
				} else {
					oObj = oParentObj;
				}
				
			}			
			
			$.post('/admin/short_urls_ajax/get_version_number', oParams, function(data) {
				if (oShortUrl.version_id === 0) {
					$.prompt('<?php echo addslashes($aMessages['admin_short_url_created']); ?>');
				} else {
					$.prompt('<?php echo addslashes($aMessages['admin_short_url_updated']); ?>');
				}
				oShortUrl.version_id = data.iVersionId;
				
			}, 'json');
		});
		
	}
);